/*
	This is a algorithm for sorting pixels from a gray-valued image, as proposed by
	Isaac & Singleton: "Sorting by address calculations" (1956).
*/
#ifndef _FastImageSort_H_
  #define _FastImageSort_H_

#include <limits.h>
#include <assert.h>
#include "Image.H"
#include "IO.H"
#include "Util.H"
#include "..\my\Histogram.h"
#include "..\ip\rgm.h"

template <typename T>
void FIsort(const pip::Image<T> &image,std::vector<std::vector<Pixel> > &levels){
	assert(image.dim(0)-1);

	int colors(numeric_limits<T>::max()+1);
	vector<unsigned int> cumulative_histogram(colors); 
	levels.resize(colors);
	
	getHistogram(image,cumulative_histogram,true);
	
	for(int z=0; z<image.dim(2);++z){
		for(int y=0; y<image.dim(1);++y){
			for(int x=0; x<image.dim(0);++x){
				levels[image(x,y,z)].push_back(Pixel(x,y,z));
			}
		}
	}
}

#endif 